package com.heatup.apt.management.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.common.util.StringUtil;
import com.heatup.apt.management.service.custom.CustomService;
import com.heatup.apt.management.service.custom.UserAccountRechargeService;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.user.UserBlacklist;

import net.sf.json.JSONObject;

/**
 * 客户管理控制器
 * @author maxxiang
 * 2016年9月23日 下午4:07:20
 */

@Controller
@RequestMapping("system/custom")
public class CustomController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(CustomController.class);
	
	@Autowired
	private CustomService  customService;
	
	@Autowired
	private UserAccountRechargeService userAccountRechargeService;
	
    @RequestMapping(value = "to_custom_list")
    public ModelAndView toCustomList(HttpServletRequest request, HttpServletResponse response) {
    	ModelAndView model = new ModelAndView();
    	model.addObject("userInfo", this.getUserInfo());
        model.setViewName("custom/custom-list");
        return model;
    }
    
    /**
     * 查询客户列表
     * CustomController.user_list()<BR>
     * <P>Author :  maxxiang </P>  
     * <P>Date : 2016年9月30日 </P>
     * @param request
     * @param response
     */
    @RequestMapping(value = "custom_list")
    public void custom_list(HttpServletRequest request, HttpServletResponse response) {
    	
    	Object pageIndex = request.getParameter("pageIndex");
    	Object pageSize = request.getParameter("pageSize");
    	String mobile = request.getParameter("mobile");
    	Object customLevel = request.getParameter("customLevel");
    	String nickName = request.getParameter("nickName");
    	Integer id = RequestTools.RequestInt(request, "id", 0);
    	if (StringUtil.isEmpty(pageIndex)) {
    		pageIndex = 0;
		}
    	if (StringUtil.isEmpty(pageSize)) {
    		pageSize = 20;
    	}
    	int page = Integer.valueOf(pageIndex.toString());
    	if (Integer.valueOf(pageIndex.toString()) > 1) {
			page = page - 1;
		}else{
			page = 0;
		}
    	
    	Map<Object, Object> map = customService.queryCustomList(page, Integer.valueOf(pageSize.toString()), mobile.toString(), Integer.valueOf(customLevel.toString()),nickName,id);
    	JSONObject jsonObject = new JSONObject();
    	jsonObject.accumulate("custom_list", map.get("custom_list"));
    	jsonObject.accumulate("custom_count", (Integer)map.get("custom_count"));
    	jsonObject.accumulate("pageIndex", page >0?pageIndex:1);
    	jsonObject.accumulate("pageSize", pageSize);
    	jsonObject.accumulate("mobile", mobile);
    	jsonObject.accumulate("customLevel", customLevel);
    	jsonObject.accumulate("nickName", nickName);
    	jsonObject.accumulate("id", id);
    	jsonObject.accumulate("userInfo", this.getUserInfo());
    	responseSendMsg(response, jsonObject.toString());
    }
    
    @ResponseBody
    @RequestMapping("pull_blacklist")
    public String pullBlacklist(UserBlacklist userBlacklist, HttpServletRequest request){
    	ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
    	String callback = RequestTools.RequestString(request, "callback", "");
    	try {
			boolean bool = customService.userPullBlacklist(userBlacklist, this.getUserInfo());
			if(bool){
				apiResultModel.setRet(apiResultModel.RET_SUCCESS);
				apiResultModel.setMsg("成功拉黑！");
				apiResultModel.setData("成功拉黑！");
			}else{
				apiResultModel.setRet(apiResultModel.RET_ERROR);
				apiResultModel.setMsg("拉黑失败！");
				apiResultModel.setData("拉黑失败！");
			}
		} catch (Exception e) {
			// TODO: handle exception
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			apiResultModel.setMsg("系统异常");
			apiResultModel.setData("系统异常！");
			logger.error("================= pullBlacklist error :"+e.toString()+e.getCause(),e);
		}
    	return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
    }
    
}
